<template>
  <el-upload
    class="avatar-uploader"
    action="#"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
    :http-request="upload"
  >
    <i class="el-icon-circle-close" ></i>
    <img v-if="imageUrl" :src="imageUrl" class="avatar">
    <i v-else class="avatar-uploader-icon">上传图片</i>
    <!-- <el-progress v-if="showProgress" type="circle" :percentage="percent" class="progress" /> -->
  </el-upload>
</template>

<script>
// 引入必要的COS模块
const COS = require('cos-js-sdk-v5')
// 实例化对象
const cos = new COS({
  SecretId: 'AKIDGQWWRGDziJkz3ub7SinEYmR9mJkJgu1c', // 身份识别ID
  SecretKey: 'WwGTXaWO6e5To5GZqETWiq08DC6sW8wj' // 身份秘钥
})
export default {
  name: 'UploadImg',
  props: {
    code: {
      default: '',
      type: String
    },
    img: {
      default: '',
      type: String
    }
  },
  data () {
    return {
      imageUrl: '',
      percent: 0,
      showProgress: false
    }
  },
  watch: {
    img: function () {
      this.imageUrl = this.img
      console.log(this.imageUrl)
    }
  },
  methods: {
    // dianji (file) {
    //   console.log(111)
    //   // 1.获取将要删除图片的临时路径
    //   const filePath = file.response.data.tmp_path
    //   // 2.从pics数组中，找到图片对应的索引值
    //   const i = this.img.findIndex((x) => x.img === filePath)
    //   // 3.调用splice方法，移除图片信息
    //   this.img.splice(i, 1)
    // },
    // 处理移除图片的操作
    // handleRemove (file) {
    //   // 1.获取将要删除图片的临时路径
    //   const filePath = file.response.data.tmp_path
    //   // 2.从pics数组中，找到图片对应的索引值
    //   const i = this.img.findIndex((x) => x.img === filePath)
    //   // 3.调用splice方法，移除图片信息
    //   this.img.splice(i, 1)
    // },
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isPNG) {
        this.$message.error('上传头像图片只能是 png 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isPNG && isLt2M
    },
    upload (res) {
      if (res.file) {
        // 显示
        this.showProgress = true
        // 执行上传操作
        cos.putObject({
          Bucket: 'wangchao-1313015829', /* 存储桶 */
          Region: 'ap-beijing', /* 存储桶所在地域，必须字段 */
          Key: res.file.name, /* 文件名 */
          StorageClass: 'STANDARD', // 上传模式, 标准模式
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            console.log(JSON.stringify(progressData))
            this.percent = progressData.percent * 100 // 进度条
          }
        }, (err, data) => {
          console.log(err || data)
          // 上传成功之后
          if (data.statusCode === 200) {
            this.imageUrl = `https://${data.Location}`
            // 执行这个自定义事件把url传出去
            this.$emit('seachimg', this.imageUrl, this.code)
            // 隐藏
            this.showProgress = false
          }
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
// 上传图片
.option_item .avatar-uploader {
  position: relative;
  width: 100px;
  height: 60px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  margin-left: 4px;
  display: inline-block;
  margin-bottom: 10px;
}
.avatar-uploader .el-upload {
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 14px;
  color: #303133;
  width: 100px;
  height: 60px;
  line-height: 60px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 60px;
  display: inline-block;
  margin-left: -22px;
}
.el-icon-circle-close {
  z-index: 999;
  position: absolute;
  right: 0;
  top: 0;
  -webkit-transform: translate(50%, -50%);
  transform: translate(50%, -50%);
  background: #fff;
  font-size: 18px;
  color: #999;
}
.el-icon-circle-close:hover {
  color: rgb(234, 28, 28);
}
</style>
